package com.mingmen.canting.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.mingmen.canting.R;
import com.mingmen.canting.defineview.GlideRoundTransform;
import com.mingmen.canting.httpconfig.AppConfig;
import com.mingmen.canting.model.DaTingModel;

import java.util.ArrayList;
import java.util.List;


public class SupplierDaTingAdapter extends RecyclerView.Adapter<SupplierDaTingAdapter.ViewHolder> {
    List<DaTingModel.DataBean> myLiveList = new ArrayList<>();
    Context mContext;
    private OnItemClickListener mOnItemClickListener;
    private int checkedPosition = -1;
    String supplierId;

    //recycleview标签
    public void setCheckedPosition(int checkedPosition) {
        this.checkedPosition = checkedPosition;
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClickListener(View view, int pos, List<DaTingModel.DataBean> myLiveList);
    }
    public SupplierDaTingAdapter(Context context, List<DaTingModel.DataBean> myLiveList, String supplierId) {
        this.mContext = context;
        this.myLiveList = myLiveList;
        this.supplierId = supplierId;
    }

    public void setDataList(List<DaTingModel.DataBean> list) {
        myLiveList = list;
        notifyDataSetChanged();
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_supplierdatingl, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }
    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        DaTingModel.DataBean dataBean = myLiveList.get(position);
        holder.commodityName.setText(myLiveList.get(position).getSupplierName());
        holder.address.setText("地址: " + dataBean.getAddress());
        holder.sales.setText("月销量: " + dataBean.getSales() + "");
        holder.score.setText(dataBean.getScore() + "分");
        holder.category.setText(dataBean.getCategory());
        Glide.with(mContext).load(AppConfig.IMGURL + dataBean.getLogo()).dontAnimate().placeholder(R.mipmap.zhanwei).error(R.mipmap.zhanwei).transform(new GlideRoundTransform(mContext, 5)).into(holder.ivAvatar);
        if (dataBean.getIsCooperate().equals("0")) {
            holder.btn_isCooperate.setBackground(mContext.getResources().getDrawable(R.drawable.shape_normal_weihezuo));
            holder.btn_isCooperate.setText("未合作");
        } else {
            if (supplierId.equals("2") && myLiveList.get(position).getScore() >= 5) {
                holder.content.setVisibility(View.GONE);
            } else {
                holder.btn_isCooperate.setBackground(mContext.getResources().getDrawable(R.drawable.shape_normal_yihezuo));
                holder.btn_isCooperate.setText("已合作");
            }
        }
        if (dataBean.getIsShipping().equals("0")) {
            holder.isShipping.setText("不支持配送");
        } else {
            holder.isShipping.setText("支持配送");
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onItemClickListener(v, position, myLiveList);
            }
        });
        holder.btn_Phone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onItemClickListener(v, position, myLiveList);
            }
        });
    }

    @Override
    public int getItemCount() {
        return myLiveList.size();
    }
    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView commodityName;
        LinearLayout content;
        TextView address;
        TextView sales;
        TextView score;
        TextView isShipping;
        TextView category;
        TextView btn_isCooperate;
        ImageView ivAvatar;
        Button btn_Phone;
        public ViewHolder(View itemView) {
            super(itemView);
            commodityName = (TextView) itemView.findViewById(R.id.commodityName);
            content = itemView.findViewById(R.id.content);
            address = itemView.findViewById(R.id.address);
            sales = itemView.findViewById(R.id.sales);
            score = itemView.findViewById(R.id.score);
            isShipping = itemView.findViewById(R.id.isShipping);
            category = itemView.findViewById(R.id.category);
            btn_isCooperate = itemView.findViewById(R.id.btn_isCooperate);
            ivAvatar = itemView.findViewById(R.id.ivAvatar);
            btn_Phone = itemView.findViewById(R.id.btn_Phone);
        }
    }
}
